package com.ruoyi.utils;

import com.ruoyi.book.domain.Fiction;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Jsoup 工具类
 */
public class JsoupUtil {

    private static final Logger logger = LoggerFactory.getLogger(JsoupUtil.class);

    /**
     * @param content
     * @return 删除Html标签
     */
    public static String delHTMLTag(String content) {
        content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");

        // 去除字符串中的空格 回车 换行符 制表符 等

        content = content.replaceAll("\\s*|\t|\r|\n", "");

        // 去除空格
        content = content.replaceAll("&nbsp;", "");

        return content;
    }

    /**
     * 字符串转化为UTF-8
     *
     * @param str
     * @return
     */
    public static String toUTF8(String str) {
        String result = str;
        try {
            result = changeCharSet(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * @param str
     * @param newCharset
     * @return
     * @throws UnsupportedEncodingException
     */
    private static String changeCharSet(String str, String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            // 用默认字符编码解码字符串。
            byte[] bs = str.getBytes();
            // 用新的字符编码生成字符串
            return new String(bs, newCharset);
        }
        return str;
    }

    /**
     * @param str
     * @return
     */
    public static String sub(String str) {

        if (!str.isEmpty()) {
            return str.substring(str.indexOf("：") + 1);
        }
        return null;
    }

    public static String subContent(String str) {

        if (!str.isEmpty()) {
            return str.substring(0, str.indexOf("http"));
        }
        return null;
    }

    /**
     * 获取链接的document对象
     *
     * @param url url
     * @return document
     */
    public static Document getDoc(String url) {
        boolean flag;
        Document document = null;
        int i = 0;
        do {
            try {
                document = Jsoup.connect(url)
                        .header("Host", "https://www.dampmeta.com")
                        .header("Accept",
                                "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language", "zh-CN,zh;q=0.9")
                        .header("Cache-Control", "no-cache")
                        .header("Connection", "keep-alive")
                        .header("Pragma", "no-cache")
                        .header("Upgrade-Insecure-Requests", "1")
                        .timeout(5000)
                        .userAgent("Mozilla")//模拟浏览器
                        .get();
                flag = false;
            } catch (IOException e) {
                i++;
                logger.info("获取document失败重试{}次", i);
                if (i == 100) {
                    flag = false;
                } else {
                    flag = true;
                }
            }
        } while (flag);
        return document;
    }

    public static void main(String[] args) {
        Document doc = getDoc("https://www.dampmeta.com/book/14509.html");
        Elements small = doc.select(".container .border3-2 .info-main .lazy");
        String dataOriginal = small.first().attr("data-original");
        System.out.println("doc = " + doc);
    }

    public static boolean isConnection(String url) {
        boolean flag = false;
        int counts = 0;
        if (null == url || url.length() <= 0) {
            return flag;
        }
        while (counts < 10) {
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                int state = connection.getResponseCode();
                if (state == 200 || state == 403) {
                    flag = true;
                }
                break;
            } catch (Exception e) {
                counts++;
            }
        }
        return flag;
    }



//
//    /**
//     * 抓取小说持久化到mysql数据库
//     *
//     * @param fictionURL
//     */
//    @Async
//    @Transactional(rollbackFor = RuntimeException.class)
//    @Override
//    public void insert(String fictionURL) {
//
//        //爬取小说基本信息
//        Fiction fiction = getFictions(JsoupUtil.getDoc(fictionURL));
//        if (fiction != null) {
//            //查询数据库信息
//            QueryWrapper<Fiction> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("fiction_name", fiction.getFictionName());
//            Fiction ft = iFictionService.getOne(queryWrapper);
//            if (ft == null) {
//                //设置url
//                fiction.setFictionUrl(fictionURL);
//                iFictionService.save(fiction);
//                Document doc = JsoupUtil.getDoc(fictionURL);
//                Elements elements = doc.select(".container .info-chapters").get(0).getAllElements();
//                log.info("小说章数：" + elements.size());
//                //前面12章节重复
//                int k = 12;
//                //数据库排序
//                int sort = 1;
//                //前面12章节重复
//                int j = elements.size() - 12;
//                ChapterContent chapterContent = new ChapterContent();
//                for (int i = 0; i < j; i++) {
//                    try {
//                        String url = elements.get(k).attr("abs:href");
//                        Document document = JsoupUtil.getDoc(url);
//                        //章节标题
//                        String title = document.select("h1").text();
//                        //章节内容
//                        String text = document.getElementById("article").html();
//                        //保存小说内容
//                        chapterContentMapper.insert(chapterContent.setContent(text));
//                        int id = chapterContent.getId();
//                        chapterMapper.insert(new Chapter(url, fiction.getId(), title, sort, id));
//                        log.info("抓取小说：" + fiction.getFictionName() + "" + title);
//                        sort++;
//                        k++;
//                    } catch (Exception e) {
//                        log.error("抓取小说内容异常" + e);
//                    }
//                }
//                log.info("抓取小说完成");
//            } else {
//                log.info("这本小说已经保存到数据库");
//            }
//        } else {
//            log.info("小说连接地址错误");
//        }
//    }
//
    /**
     * 获取书趣阁小说基本信息
     *
     * @param document
     * @return
     */
    public static Fiction getFictions(Document document) {
        boolean flag = false;
        try {
            //小说基本信息
            String readUrl = document.select("meta[property=og:novel:read_url]").attr("content");
            String fictionName = document.select("meta[property=og:novel:book_name]").attr("content");
            String brief = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String type = document.select("meta[property=og:novel:category]").attr("content");
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("#fmimg > img").attr("abs:src");
//            Elements imgElements = document.select(".container .border3-2 .info-main .lazy");
//            String img = imgElements.first().attr("data-original");
            //字数
//                String number = JsoupUtil.sub(small.get(0).child(3).text());
            return new Fiction(readUrl, img, brief, fictionName, author, type, newest, state, "");
        } catch (Exception e) {
            logger.error("获取小说基本信息失败" + e);
        }
        return null;
    }


}
